//! @author Adam Emil Skoog
//! @date   2012-01-05

#include "NPC.h"
#include "Game.h"
#include "Mehdi.h"
#include <cmath>

namespace mehdi
 {
	NPC::NPC()
	:
	image(NULL),
	name ("?"),
	label("?",sf::Font::GetDefaultFont(),30.f)
	 {
		if (!bubble)
			bubble = Game::getImageLoader()->get("bubble");

		setReceptiveToGravity(false);
		setReceptiveToCollision(false);

		label.SetColor(sf::Color(200,200,10));
	 }

	void NPC::draw() const
	 {
		if (getImage())
		 {
			getImage()->SetPosition(position());
			image->SetScale(1.f + std::sin(Game::getPlaytime() * 8.f) * .01f,
			                1.f + std::sin(Game::getPlaytime() * 4.f) * .02f);
			Game::getWindow()->Draw(*getImage());

			label.SetPosition(getImage()->GetPosition().x + getImage()->getImage()->GetWidth() / 2.f -
			                  label.GetRect().GetWidth() / 2.f,
			                  getImage()->GetPosition().y - label.GetRect().GetHeight() * 2.f -
							  std::sin(Game::getPlaytime() * 4.f) * 2.f);
			Game::getWindow()->Draw(label);

			if (std::abs(Mehdi::getCurrentMehdi()->position().x - position().x) <= 155.f)
			 {
				bubble->SetPosition(position().x - bubble->getImage()->GetWidth() - 10.f,
				                    position().y + getImage()->getImage()->GetHeight() / 2.f -
									bubble->getImage()->GetHeight() / 2.f -
							        std::sin(Game::getPlaytime() * 4.f) * 2.f);
				Game::getWindow()->Draw(*bubble);
			 }
		 }
	 }

	NPC &NPC::setImage(Image *const image)
	 {
		this->image = image;
		return *this;
	 }

	Image *const NPC::getImage() const
	 {
		return image;
	 }

	NPC &NPC::setName(const std::string &name)
	 {
		if (this->name != name)
		 {
			this->name = name;
			label.SetText(name);
		 }

		return *this;
	 }

	const std::string &NPC::getName() const
	 {
		return name;
	 }

	Image *NPC::bubble = NULL;
 }
